﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="App_Code.Modules.BookModule" %>
<%@ Import Namespace="Portal.Core.Binding" %>
<%@ Import Namespace="Portal.Core.Helper" %>
<%@ Import Namespace="Portal.Core.Utils" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register Assembly="Portal.Core" Namespace="Portal.Core.Web.UI.Controls" TagPrefix="cc1" %>
<asp:PlaceHolder runat="server" ID="phContent">
    <div class="filter">
        <h3>Thêm truyện</h3>
    </div>
    <table class="Form">
        <tr>
            <td style="width: 15%">Thể loại
            </td>
            <td>
                <asp:ListBox ID="HistyoryIds" Height="200" runat="server" DataValueField="Id" DataTextField="FullName" SelectionMode="Multiple"></asp:ListBox>
            </td>
        </tr>
        <tr>
            <td style="width: 15%">Tác giả
            </td>
            <td>
                <asp:TextBox runat="server" ID="author" Width="250"></asp:TextBox>
                <em>(Gõ vào ô text để tìm chính xác tác giả, hoặc tạo mới nếu chưa tồn tại)</em>
            </td>
        </tr>
        <tr>
            <td>Tiêu đề <span class="red bold">(*)</span>
            </td>
            <td>
                <asp:TextBox ID="FullName" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Tiêu đề khác
            </td>
            <td>
                <asp:TextBox ID="OtherName" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>OtherSortName
            </td>
            <td>
                <asp:TextBox ID="OtherSortName" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Mô tả
            </td>
            <td>
                <telerik:RadEditor ID="Description" runat="server" ToolsFile="~/cms_client/Editor_Rad_Full.xml" Height="350" Width="99%">
                </telerik:RadEditor>
            </td>
        </tr>
        <tr>
            <td>Ảnh
            </td>
            <td>
                <cc1:ImageUpload ID="Image" runat="server" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:CheckBox ID="Status" Text="Kích hoạt?" runat="server" Checked="true" />
                <asp:CheckBox ID="IsUnique" Text="Duy nhất?" runat="server" Checked="true" />
                <asp:CheckBox ID="IsHot" Text="Truyện HOT?" runat="server" />
                <asp:CheckBox ID="Finish" Text="Đã kết thúc?" runat="server" />
            </td>
        </tr>
        <tr>
            <td colspan="2">---------- SEO ------------------------------------------------------------------------------------------------</td>
        </tr>
        <tr>
            <td>MetaTitle</td>
            <td>
                <asp:TextBox ID="MetaTitle" runat="server" Width="90%" Rows="2" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>MetaDesc</td>
            <td>
                <asp:TextBox ID="MetaDesc" runat="server" Width="90%" Rows="2" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>MetaKeyword</td>
            <td>
                <asp:TextBox ID="MetaKeyword" runat="server" Width="90%" Rows="2" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Tags</td>
            <td>
                <asp:TextBox ID="Tags" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">---------- Crawl Info ------------------------------------------------------------------------------------------------</td>
        </tr>
        <tr>
            <td>Nguồn</td>
            <td>
                <asp:TextBox ID="Source" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>CrawlName</td>
            <td>
                <asp:TextBox ID="CrawlName" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>CrawlHost</td>
            <td>
                <asp:TextBox ID="CrawlHost" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>CrawlAuthor</td>
            <td>
                <asp:TextBox ID="CrawlAuthor" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>CrawlUrl</td>
            <td>
                <asp:TextBox ID="CrawlUrl" runat="server" Width="90%"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <br/>
                <asp:Button ID="Save" runat="server" Text="Save" OnClientClick="return checkValid();" OnClick="Save_Click" />
                <asp:Button ID="Cancel" runat="server" OnClick="Cancel_Click" Text="Cancel" ValidationGroup="xxxx" />
            </td>
        </tr>
    </table>

</asp:PlaceHolder>

<script type="text/javascript">
    $(function () {
        $("#<%=author.ClientID%>").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Ajax.aspx",
                    dataType: "json",
                    data: { p: "Author", act: "find", key: request.term },
                    success: function (data) {
                        response(data.label);
                    }
                });
            }
        });
    });
    function checkValid() {
        var types = $('#<%=HistyoryIds.ClientID %>');
        if (types.val() == null) {
            alert("Hãy chọn thể loại truyện!");
            types.focus();
            return false;
        }
        var author = jQuery('#<%=author.ClientID %>');
        if (author.val() == null) {
            alert("Hãy chọn tác giả!");
            author.focus();
            return false;
        }
        var fullname = jQuery('#<%=FullName.ClientID %>');
        if (fullname.val().length == 0) {
            alert("Hãy điền tiêu đề!");
            fullname.focus();
            return false;
        }
        return true;
    }
</script>

<script runat="server">
    protected override string Action
    {
        get { return "newest"; }
    }

    private Book _book;
    protected Book NewBook
    {
        get
        {
            if (_book == null)
            {
                object obj = Binder.GetData(typeof(Book), phContent, new BindContext());
                if (obj != null)
                {
                    _book = (Book)obj;
                }
            }
            return _book;
        }
    }
    protected override void OnLoad(EventArgs e)
    {
        if (!IsPostBack)
        {
            SetRadEditorPlugin(Description);
            HistyoryIds.DataSource = HistoryType.ListAll();
            HistyoryIds.DataBind();
        }
    }
    void SetRadEditorPlugin(RadEditor editor)
    {
        editor.DisableFilter(EditorFilters.ConvertCharactersToEntities | EditorFilters.RemoveScripts);
        editor.EditModes = EditModes.Design | EditModes.Html;
        editor.StripFormattingOptions = EditorStripFormattingOptions.MSWord |
                                                            EditorStripFormattingOptions.MSWordNoFonts |
                                                            EditorStripFormattingOptions.Css |
                                                            EditorStripFormattingOptions.Font |
                                                            EditorStripFormattingOptions.Span |
                                                            EditorStripFormattingOptions.ConvertWordLists;
    }
    protected override void Save_Click(object sender, EventArgs e)
    {
        if (NewBook.Description.Length > MaxLenght)
            NewBook.Description = Process.ConvertToText(NewBook.Description, MaxLenght - 3);

        var authorName = author.Text.Trim();
        var authSortName = Process.ConvertToSafeMarks(authorName).Replace("-", "").Trim().ToLower();
        var auth = Author.MatchedBySortName(authSortName);
        if (auth == null)
        {
            auth = new Author { FullName = authorName, SortName = authSortName };
            auth.Save();
        }
        NewBook.AuthorId = auth.ID;

        //check image, and upload to google
        if (!string.IsNullOrEmpty(NewBook.Image))
        {
            var bloggerAcc = GoogleHelper.GetBloggerAccount();
            try
            {
                var gInfos = GoogleHelper.UploadImageToGoogle(FileHelper.GetPhysicalPath(NewBook.Image), NewBook.SortName, bloggerAcc.UserName, bloggerAcc.Password, "truyengicungco");
                if (gInfos.Count > 0)
                {
                    var googleImage = gInfos["url"].ToString();
                    //remove image temps here...
                    FileHelper.Remove(NewBook.Image);
                    //set new full google path
                    NewBook.Image = googleImage;
                }
            }
            catch { }
        }
        
        NewBook.Save();
        
        //var listSelected = (from ListItem item in OtherCategories.Items where item.Selected select Convert.ToInt32(item.Value)).ToList();
        
        Forward("Save");
    }
</script>